package com.rs2hd.packethandler;

import com.rs2hd.model.Player;
import com.rs2hd.net.Packet;
import org.apache.mina.common.IoSession;

/**
 * Handles walking packets.
 *
 * @author Graham
 */
public class WalkPacketHandler implements PacketHandler {

	@Override
	public void handlePacket(Player player, IoSession session, Packet packet) {
		player.getWalkingQueue().reset();
		player.setAttacking(false);
		player.getCombat().attackNpc = false;
		player.getUpdateFlags().setFaceToUpdateRequired(true, 0);
		player.resetSkills();
		player.following = false;
		if(player.frozen > 0) {
			player.getActionSender().sendMessage("You Are Frozen!.");
			player.getWalkingQueue().reset();
			return;
		}
		int toY = packet.readLEShort() - (player.getLocation().getRegionY() - 6) * 8;
		packet.readByte();
		int toX = packet.readShort() - (player.getLocation().getRegionX() - 6) * 8;
		int[][] path = findRoute(player, toX, toY);

		if (path != null) {
			for (int i = 0; i < path.length; i++) {
				player.getWalkingQueue().addToWalkingQueue(path[i][0], path[i][1]);
			}
		}
	}

	private int[][] findRoute(Player p, int toX, int toY) {
		return new int[][]{new int[]{toX, toY}};
	}
}